﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Security;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml;

    public class AdminMenu : UserControl
    {
        protected BaseModule _Parent;
        protected string dnnctl;
        protected string etmode;
        protected PlaceHolder plhSubTabMenu;
        protected PlaceHolder plhTabMenu;
        protected PlaceHolder plhTitle;
        protected bool skinurl;
        protected string spg;

        public AdminMenu()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
            this.dnnctl = "";
            this.spg = "";
            this.etmode = "";
            this.skinurl = false;
        }

        private void AddCSSLink(XmlDataDocument xmlDoc)
        {
            string str = this.getAtt(xmlDoc, "root/css", "path");
            if (str != "")
            {
                HtmlGenericControl child = new HtmlGenericControl("link");
                child.Attributes["rel"] = "stylesheet";
                child.Attributes["media"] = "screen";
                child.Attributes["type"] = "text/css";
                child.Attributes["href"] = str;
                Control control = this.Page.FindControl("CSS");
                if (control != null)
                {
                    control.Controls.Add(child);
                }
            }
        }

        private void buildEnterButton(XmlDataDocument xmlDoc)
        {
            HyperLink child = new HyperLink();
            string resourceCtrl = this.getAtt(xmlDoc, "root/resx", "name");
            string str6 = DotNetNuke.Common.Globals.QueryStringEncode(DotNetNuke.Common.Globals.ResolveUrl(this.getAtt(xmlDoc, "root/skin", "path")));
            string str = this.getAtt(xmlDoc, "root/enterbutton", "css");
            string str2 = this.getAtt(xmlDoc, "root/enterbutton", "cssdiv");
            string tabText = this.getAtt(xmlDoc, "root/enterbutton", "resourcekey");
            string controlKey = this.getAtt(xmlDoc, "root/enterbutton", "defaultctrl");
            child.Text = this.getLocalTabText(tabText, resourceCtrl);
            if ((child.Text != "") & resourceCtrl.StartsWith(this.Parent.GetType().BaseType.Name))
            {
                child.CssClass = str;
                child.NavigateUrl = DotNetNuke.Common.Globals.NavigateURL(controlKey, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), "SkinSrc=" + str6 });
                if (str2 != "")
                {
                    str2 = " class=\"" + str2 + "\"";
                }
                this.plhTitle.Controls.Add(new LiteralControl("<div " + str2 + ">"));
                this.plhTitle.Controls.Add(child);
                this.plhTitle.Controls.Add(new LiteralControl("</div>"));
            }
        }

        private void buildMenu(XmlDataDocument xmlDoc)
        {
            XmlNode current;
            IEnumerator enumerator=null;
            string str12 = "";
            string str7 = "";
            string resourceCtrl = this.getAtt(xmlDoc, "root/resx", "name");
            string str2 = DotNetNuke.Common.Globals.QueryStringEncode(DotNetNuke.Common.Globals.ResolveUrl(this.getAtt(xmlDoc, "root/skin", "path")));
            string str14 = this.getAtt(xmlDoc, "root/css", "titlediv");
            string str9 = this.getAtt(xmlDoc, "root/css", "tabdiv");
            string str13 = this.getAtt(xmlDoc, "root/css", "tabul");
            string str11 = this.getAtt(xmlDoc, "root/css", "tabli");
            string str10 = this.getAtt(xmlDoc, "root/css", "tabliactive");
            string str4 = this.getAtt(xmlDoc, "root/css", "subtabdiv");
            string str8 = this.getAtt(xmlDoc, "root/css", "subtabul");
            string str6 = this.getAtt(xmlDoc, "root/css", "sublitabli");
            string str5 = this.getAtt(xmlDoc, "root/css", "sublitabliactive");
            if (str9 != "")
            {
                str9 = " class=\"" + str9 + "\"";
            }
            if (str13 != "")
            {
                str13 = " class=\"" + str13 + "\"";
            }
            if (str11 != "")
            {
                str11 = " class=\"" + str11 + "\"";
            }
            if (str10 != "")
            {
                str10 = " class=\"" + str10 + "\"";
            }
            if (str4 != "")
            {
                str4 = " class=\"" + str4 + "\"";
            }
            if (str8 != "")
            {
                str8 = " class=\"" + str8 + "\"";
            }
            if (str6 != "")
            {
                str6 = " class=\"" + str6 + "\"";
            }
            if (str5 != "")
            {
                str5 = " class=\"" + str6 + "\"";
            }
            if (str14 != "")
            {
                str14 = " class=\"" + str14 + "\"";
            }
            XmlNodeList list = xmlDoc.SelectNodes("root/tabs/tab");
            string text = ("<div  " + str13 + ">") + "<table  " + str9 + "><tr>";
            try
            {
                enumerator = list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    current = (XmlNode) enumerator.Current;
                    if (this.checkDisplayRoles(current))
                    {
                        bool flag = false;
                        XmlNodeList list2 = current.SelectNodes("subtab[@ctl='" + this.dnnctl + "']");
                        if ((list2 != null) && (list2.Count > 0))
                        {
                            flag = true;
                        }
                        if (this.dnnctl.ToLower() == current.Attributes["ctl"].InnerText.ToLower())
                        {
                            flag = true;
                        }
                        string innerText = current.Attributes["image"].InnerText;
                        if (!innerText.ToLower().StartsWith("~/desk"))
                        {
                            innerText = this._Parent.StoreInstallPath + innerText;
                        }
                        else
                        {
                            innerText = DotNetNuke.Common.Globals.ResolveUrl(innerText);
                        }
                        if (flag)
                        {
                            str12 = this.getLocalTabText(current.Attributes["text"].InnerText, resourceCtrl);
                            if (str2 == "")
                            {
                                text = text + "<td " + str10 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL(current.Attributes["ctl"].InnerText, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), current.Attributes["param"].InnerText }) + "\" ><img src=\"" + innerText + "\" alt=\"\" border=\"0\" align=\"absmiddle\" /> " + this.getLocalTabText(current.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                            }
                            else
                            {
                                text = text + "<td " + str10 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL(current.Attributes["ctl"].InnerText, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), current.Attributes["param"].InnerText, "SkinSrc=" + str2 }) + "\" ><img src=\"" + innerText + "\" alt=\"\" border=\"0\" align=\"absmiddle\" /> " + this.getLocalTabText(current.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                            }
                        }
                        else if (current.Attributes["ctl"].InnerText == "EXIT")
                        {
                            text = text + "<td " + str11 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL() + "\" ><img src=\"" + innerText + "\" alt=\"\" border=\"0\"  align=\"absmiddle\" /> " + this.getLocalTabText(current.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                        }
                        else
                        {
                            if (str2 == "")
                            {
                                text = text + "<td " + str11 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL(current.Attributes["ctl"].InnerText, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), current.Attributes["param"].InnerText }) + "\" ><img src=\"" + innerText + "\" alt=\"\" border=\"0\" align=\"absmiddle\" /> " + this.getLocalTabText(current.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                                continue;
                            }
                            text = text + "<td " + str11 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL(current.Attributes["ctl"].InnerText, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), current.Attributes["param"].InnerText, "SkinSrc=" + str2 }) + "\" ><img src=\"" + innerText + "\" alt=\"\" border=\"0\" align=\"absmiddle\" /> " + this.getLocalTabText(current.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            text = text + "</tr></table>" + "</div>";
            this.plhTabMenu.Controls.Add(new LiteralControl(text));
            current = xmlDoc.SelectSingleNode("root/tabs/tab/subtab[@ctl=\"" + this.dnnctl + "\"]");
            if (current == null)
            {
                current = xmlDoc.SelectSingleNode("root/tabs/tab/subtab[1]");
            }
            text = ("<div  " + str8 + ">") + "<table  " + str4 + "><tr>";
            if (current != null)
            {
                IEnumerator enumerator2=null;
                try
                {
                    enumerator2 = current.ParentNode.ChildNodes.GetEnumerator();
                    while (enumerator2.MoveNext())
                    {
                        XmlNode nod = (XmlNode) enumerator2.Current;
                        if (this.checkDisplayRoles(nod))
                        {
                            string url = nod.Attributes["image"].InnerText;
                            if (!url.ToLower().StartsWith("~/desk"))
                            {
                                url = this._Parent.StoreInstallPath + url;
                            }
                            else
                            {
                                url = DotNetNuke.Common.Globals.ResolveUrl(url);
                            }
                            if (this.IsActiveSubTab(nod.Attributes["ctl"].InnerText, this.dnnctl, nod.Attributes["param"].InnerText))
                            {
                                str7 = this.getLocalTabText(nod.Attributes["text"].InnerText, resourceCtrl);
                                if (str2 == "")
                                {
                                    text = text + "<td " + str5 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL(nod.Attributes["ctl"].InnerText, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), nod.Attributes["param"].InnerText }) + "\" ><img src=\"" + url + "\" alt=\"\" border=\"0\"  align=\"absmiddle\"/> " + this.getLocalTabText(nod.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                                }
                                else
                                {
                                    text = text + "<td " + str5 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL(nod.Attributes["ctl"].InnerText, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), nod.Attributes["param"].InnerText, "SkinSrc=" + str2 }) + "\" ><img src=\"" + url + "\" alt=\"\" border=\"0\"  align=\"absmiddle\"/> " + this.getLocalTabText(nod.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                                }
                            }
                            else if (nod.Attributes["ctl"].InnerText == "HELP")
                            {
                                text = text + "<td " + str6 + " ><a href=\"" + this._Parent.StoreInstallPath + "docs/Documentation.html\" target=\"_blank\" ><img src=\"" + url + "\" alt=\"\" border=\"0\"  align=\"absmiddle\" /> " + this.getLocalTabText(nod.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                            }
                            else
                            {
                                if (str2 == "")
                                {
                                    text = text + "<td " + str6 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL(nod.Attributes["ctl"].InnerText, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), nod.Attributes["param"].InnerText }) + "\" ><img src=\"" + url + "\" alt=\"\" border=\"0\"  align=\"absmiddle\"/> " + this.getLocalTabText(nod.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                                    continue;
                                }
                                text = text + "<td " + str6 + " ><a href=\"" + DotNetNuke.Common.Globals.NavigateURL(nod.Attributes["ctl"].InnerText, new string[] { "mid=" + Conversions.ToString(((PortalModuleBase) this.Parent).ModuleId), nod.Attributes["param"].InnerText, "SkinSrc=" + str2 }) + "\" ><img src=\"" + url + "\" alt=\"\" border=\"0\"  align=\"absmiddle\"/> " + this.getLocalTabText(nod.Attributes["text"].InnerText, resourceCtrl) + "</a></td>";
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator2 is IDisposable)
                    {
                        (enumerator2 as IDisposable).Dispose();
                    }
                }
            }
            text = text + "</tr></table>" + "</div>";
            this.plhSubTabMenu.Controls.Add(new LiteralControl(text));
            if (str7 == "")
            {
                text = "<div " + str14 + ">" + str12 + "</div>";
            }
            else
            {
                text = "<div " + str14 + ">" + str12 + " - " + str7 + "</div>";
            }
            this.plhTitle.Controls.Add(new LiteralControl(text));
        }

        private bool checkDisplayRoles(XmlNode nod)
        {
            bool flag = false;
            if ((nod.Attributes["roles"] != null) & !((PortalModuleBase) this.Parent).UserInfo.IsSuperUser)
            {
                string[] strArray = nod.Attributes["roles"].InnerText.Split(new char[] { ',' });
                int upperBound = strArray.GetUpperBound(0);
                for (int i = 0; i <= upperBound; i++)
                {
                    if (((PortalModuleBase) this.Parent).UserInfo.IsInRole(strArray[i]))
                    {
                        flag = true;
                    }
                }
                return flag;
            }
            return true;
        }

        private string getAtt(XmlDataDocument xmlDoc, string XPath, string AttName)
        {
            if (xmlDoc.SelectSingleNode(XPath).Attributes[AttName] != null)
            {
                return xmlDoc.SelectSingleNode(XPath).Attributes[AttName].InnerText;
            }
            return "";
        }

        private string getLocalTabText(string TabText, string ResourceCtrl)
        {
            string str2 = DotNetNuke.Services.Localization.Localization.GetString(Strings.Replace(TabText, " ", "_", 1, -1, CompareMethod.Binary), DotNetNuke.Services.Localization.Localization.GetResourceFile(this.Parent, ResourceCtrl));
            if (str2 == "")
            {
                str2 = TabText;
            }
            return str2;
        }

        private bool IsActiveSubTab(string MenuCtrl, string dnnCtrl, string menuaparam)
        {
            bool flag = false;
            if ((this.spg == "") & (this.etmode == ""))
            {
                if (MenuCtrl == dnnCtrl)
                {
                    flag = true;
                }
                return flag;
            }
            if ((this.spg != "") && ((MenuCtrl == dnnCtrl) & menuaparam.EndsWith(this.spg)))
            {
                flag = true;
            }
            if ((this.etmode != "") && ((MenuCtrl == dnnCtrl) & menuaparam.EndsWith(this.etmode)))
            {
                flag = true;
            }
            return flag;
        }

        private XmlDataDocument MergePlugins(int PortalID, XmlDataDocument xmlDoc)
        {
            IEnumerator enumerator=null;
            XmlDataDocument document2 = new XmlDataDocument();
            ArrayList list = new SettingsController().GetSettingList(PortalID, "None", true, "");
            try
            {
                enumerator = list.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_SettingsInfo current = (NB_Store_SettingsInfo) enumerator.Current;
                    if (current.SettingName.ToLower().EndsWith(".plugin"))
                    {
                        try
                        {
                            document2 = new XmlDataDocument();
                            document2.LoadXml(current.SettingValue);
                            string innerXml = document2.SelectSingleNode("root/tabs/tab").Attributes["id"].InnerXml;
                            string controlKey = document2.SelectSingleNode("root/tabs/tab/subtab").Attributes["ctl"].InnerXml;
                            string str2 = document2.SelectSingleNode("root/tabs/tab/subtab").Attributes["ctlsrc"].InnerXml;
                            XmlNode node2 = xmlDoc.SelectSingleNode("root/tabs/tab[@id='" + innerXml + "']");
                            if (node2 != null)
                            {
                                XmlNode node = document2.SelectSingleNode("root/tabs/tab/subtab");
                                node2.AppendChild(xmlDoc.ImportNode(node, true));
                                if (ModuleControlController.GetModuleControlByControlKey(controlKey, this._Parent.ModuleConfiguration.ModuleDefID) == null)
                                {
                                    ModuleControlInfo objModuleControl = new ModuleControlInfo {
                                        ControlKey = controlKey,
                                        ControlSrc = str2,
                                        ControlTitle = controlKey,
                                        ControlType = SecurityAccessLevel.Edit,
                                        HelpURL = "",
                                        IconFile = "",
                                        ModuleControlID = Null.NullInteger,
                                        ModuleDefID = this._Parent.ModuleConfiguration.ModuleDefID,
                                        SupportsPartialRendering = false,
                                        ViewOrder = 0
                                    };
                                    ModuleControlController.AddModuleControl(objModuleControl);
                                }
                            }
                            continue;
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exc = exception1;
                            exc.HelpLink = "Plugin Failed to Load: " + current.SettingName;
                            DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                            ProjectData.ClearProjectError();
                            continue;
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return xmlDoc;
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.plhTabMenu = new PlaceHolder();
            this.plhSubTabMenu = new PlaceHolder();
            this.plhTitle = new PlaceHolder();
            this.Controls.Add(new LiteralControl("<div class=\"NBright_MenuDiv\">"));
            this.Controls.Add(this.plhTabMenu);
            this.Controls.Add(this.plhSubTabMenu);
            this.Controls.Add(this.plhTitle);
            this.Controls.Add(new LiteralControl("</div>"));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SettingsController controller = new SettingsController();
            XmlDataDocument xmlDoc = new XmlDataDocument();
            this._Parent = (BaseModule) this.Parent;
            this.dnnctl = "";
            if (this.Request.QueryString["ctl"] != null)
            {
                this.dnnctl = this.Request.QueryString["ctl"];
            }
            this.spg = "";
            if (this.Request.QueryString["spg"] != null)
            {
                this.spg = this.Request.QueryString["spg"].ToLower();
            }
            this.etmode = "";
            if (this.Request.QueryString["etmode"] != null)
            {
                this.etmode = this.Request.QueryString["etmode"].ToLower();
            }
            this.skinurl = false;
            if (this.Request.QueryString["skinsrc"] != null)
            {
                this.skinurl = true;
            }
            string cacheKey = "";
            cacheKey = SharedFunctions.GetCacheKey("MainMenu.xml", this._Parent.PortalId);
            if (cacheKey != "")
            {
                if ((DataCache.GetCache(cacheKey) == null) | SharedFunctions.GetStoreSettingBoolean(this._Parent.PortalId, "debug.mode", "None"))
                {
                    string str3 = SharedFunctions.GetStoreSettingText(this._Parent.PortalId, "menu.xml", SharedFunctions.GetCurrentCulture(), false, true);
                    if ((str3 != "") & (Strings.InStr(str3, "ctl=\"AdminSetting\" param=\"etmode=1\"", CompareMethod.Binary) > 0))
                    {
                        xmlDoc.LoadXml(str3);
                    }
                    else
                    {
                        try
                        {
                            xmlDoc.Load(this.Server.MapPath(this._Parent.StoreInstallPath + "xml/MainMenu.xml"));
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            xmlDoc.Load(this.Server.MapPath("~/DesktopModules/NB_Store/xml/MainMenu.xml"));
                            ProjectData.ClearProjectError();
                        }
                    }
                    this.MergePlugins(this._Parent.PortalId, xmlDoc);
                    DataCache.SetCache(cacheKey, xmlDoc, DateAndTime.DateAdd(DateInterval.Hour, 1.0, DateAndTime.Now));
                }
                else
                {
                    xmlDoc = (XmlDataDocument) DataCache.GetCache(cacheKey);
                }
                string str2 = "";
                if (xmlDoc.SelectSingleNode("root/skin").Attributes["path"] != null)
                {
                    str2 = DotNetNuke.Common.Globals.QueryStringEncode(DotNetNuke.Common.Globals.ResolveUrl(xmlDoc.SelectSingleNode("root/skin").Attributes["path"].InnerText));
                }
                this.AddCSSLink(xmlDoc);
                if (this.skinurl | (str2 == ""))
                {
                    this.buildMenu(xmlDoc);
                }
                else
                {
                    this.buildEnterButton(xmlDoc);
                    SharedFunctions.RegisterJS(Conversions.ToString(this._Parent.PortalId), "jquery.nbstorejquerycheck.js", this._Parent.StoreInstallPath, this._Parent.Page);
                }
            }
        }
    }
}

